﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerCurrencyDao : ICurrencyDao
    {
        public List<Currency> GetCurrencies()
        {
            string procedures = "Get_All_Currencies";

            return Db.ReadList(procedures, true, Make, null);
        }

        public Currency GetCurrency(string currencyId)
        {
            string procedures = "Get_Currencies_ByCondition";

            object[] parms = { "@WhereClause", "WHERE CurrencyID = '" + currencyId + "'" };
            return Db.Read(procedures, true, Make, parms);
        }

        public int GetTotalPage()
        {
            string procedures = "Get_PageSize_Currency";

            object[] parms = { "@WhereClause", null };
            return Db.ReadTotalPage(procedures, true, parms);
        }

        public List<Currency> GetCurrenciesByPage(int currentPage)
        {
            string procedures = "Get_Paged_Currencies";

            object[] parms = {  "@WhereClause", null, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public List<Currency> GetCurrenciesByIsActive(bool isActive)
        {
            string procedures = "Get_Currencies_ByCondition";
            int _isActive = 0;

            if (isActive == true) { _isActive = 1; }

            object[] parms = { "@WhereClause", " WHERE IsActive = " + _isActive };
            return Db.ReadList(procedures, true, Make, parms);
        }

        public void InsertCurrency(Currency Currency)
        {
            string sql = "Insert_Currency";

            Currency.CurrencyCode = Db.Insert(sql, true, Take(Currency));
        }

        public void UpdateCurrency(Currency Currency)
        {
            string sql = "Update_Currency";

            Db.Update(sql, true, Take(Currency));
        }

        public void DeleteCurrency(Currency Currency)
        {
            string sql = "Delete_Currency";

            Db.Update(sql, true, Take(Currency.CurrencyID));
        }

        private object[] Take(string currencyId)
        {
            return new object[]  
            {
                "@CurrencyID", currencyId,
            };
        }

        private object[] Take(Currency Currency)
        {
            return new object[]  
            {
                "@CurrencyID", Currency.CurrencyID,
                "@CurrencyCode", Currency.CurrencyCode,
                "@CurrencyName", Currency.CurrencyName,
                "@Description", Currency.Description,
                "@IsActive", Currency.IsActive
            };
        }

        private static Func<IDataReader, Currency> Make = reader =>
            new Currency
            {
                CurrencyID = reader["CurrencyID"].AsString(),
                CurrencyCode = reader["CurrencyCode"].AsString(),
                CurrencyName = reader["CurrencyName"].AsString(),
                Description = reader["Description"].AsString(),
                IsActive = reader["IsActive"].AsBool()
            };
    }
}
